package jl.com.util;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AddressUtils {


    /**
     * 获取本机的内网ip地址
     *
     * @return
     * @throws SocketException
     */
    public static String getInnetIp() throws SocketException {
        String localip = null;// 本地IP，如果没有配置外网IP则返回它
        String netip = null;// 外网IP
        Enumeration<NetworkInterface> netInterfaces;
        netInterfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress ip = null;
        boolean finded = false;// 是否找到外网IP
        while (netInterfaces.hasMoreElements() && !finded) {
            NetworkInterface ni = netInterfaces.nextElement();
            Enumeration<InetAddress> address = ni.getInetAddresses();
            while (address.hasMoreElements()) {
                ip = address.nextElement();
                if (!ip.isSiteLocalAddress()
                        && !ip.isLoopbackAddress()
                        &&ip.getHostAddress().indexOf(":") == -1){// 外网IP
                    netip = ip.getHostAddress();
                    finded = true;
                    break;
                } else if (ip.isSiteLocalAddress()
                        && !ip.isLoopbackAddress()
                        && ip.getHostAddress().indexOf(":") == -1) {// 内网IP
                    localip = ip.getHostAddress();
                }
            }
        }
        if (netip != null && !"".equals(netip)) {
            return netip;
        } else {
            return localip;
        }
    }

    /**
     * 获取本机的外网ip地址
     *
     * @return
     */
    public static String getV4IP() {
        String ip = "";
        String chinaz = "http://ip.chinaz.com";

        StringBuilder inputLine = new StringBuilder();
        String read = "";
        URL url = null;
        HttpURLConnection urlConnection = null;
        BufferedReader in = null;
        try {
            url = new URL(chinaz);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
            while ((read = in.readLine()) != null) {
                inputLine.append(read + "\r\n");
            }
//System.out.println(inputLine.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
// TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
        Matcher m = p.matcher(inputLine.toString());
        if (m.find()) {
            String ipstr = m.group(1);
            ip = ipstr;
//System.out.println(ipstr);
        }
        return ip;
    }


    /**
     * 路由器重启
     */
    @Test
    public void restart(){
        System.setProperty("webdriver.chrome.driver", "Tools/chromedriver.exe");//Tools转移至tomcat bin目录下
        WebDriver driver =new ChromeDriver();
        driver.get("http://192.168.1.1");
        String ip1="";
        String ip2 ="";
        try {
            System.out.println("重启前:");
            ip1 = AddressUtils.getInnetIp(); //局域网的ip地址，比如：192.168.1.101
            System.out.println("内网ip:"+ip1);
            ip2 = AddressUtils.getV4IP(); //用于实际判断地址的ip
            System.out.println("外网ip:"+ip2);

            Thread.sleep(3000);
            driver.findElement(By.id("lgPwd")).sendKeys("123456"); //id 密码
            Thread.sleep(3000);
            driver.findElement(By.className("btnR")).click(); //id 确认登录
            Thread.sleep(3000);
            driver.findElement(By.xpath("//*[@id=\"headFunc\"]/li[2]")).click(); //class 高级设置
            Thread.sleep(3000);
            driver.findElement(By.xpath("//*[@id=\"highSetMenu\"]/ul[7]")).click(); //xpath 设备管理器
            Thread.sleep(3000);
            driver.findElement(By.id("sysTool_menu5")).click();  //id 重启路由器 菜单
            Thread.sleep(3000);
            driver.findElement(By.id("sysReboot")).click();
            Thread.sleep(3000);
//            driver.findElement(By.className("cancel")).click(); //取消重启路由器
            driver.findElement(By.className("subBtn")).click(); //确定重启路由器
            Thread.sleep(3000);
            Thread.sleep(120000);

            driver.get("https://www.baidu.com/");
            System.out.println("重启后:");
            ip1 = AddressUtils.getInnetIp(); //局域网的ip地址，比如：192.168.1.101
            System.out.println("内网ip:"+ip1);
            ip2 = AddressUtils.getV4IP(); //用于实际判断地址的ip
            System.out.println("外网ip:"+ip2);

            Thread.sleep(5000);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            driver.quit();
        }

    }

    /**
     * 查看内网、外网ip
     */
    @Test
    public  void ipTest() {
        //step1.获得内网ip和外网ip，并输出到控制台
        String ip1="";
        try {
            ip1 = AddressUtils.getInnetIp(); //局域网的ip地址，比如：192.168.1.101
        } catch (SocketException e1) {
            e1.printStackTrace();
        }
        System.out.println("内网ip:"+ip1);
        String ip2 = AddressUtils.getV4IP(); //用于实际判断地址的ip
        System.out.println("外网ip:"+ip2);
    }

}
